package com.javatest.ajax.app1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-09-03 下午3:13
 */
public class ValidateUserNameServlet extends HttpServlet {
    private static final long serialVersionUID = 3081832619811835574L;
    private static final Logger LOGGER = LoggerFactory.getLogger(ValidateUserNameServlet.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<String> nameList = Arrays.asList("aaa", "bbb", "ccc");
        String username = req.getParameter("db.username");
        String result = null;
        if (nameList.contains(username)) {
            result = "<font color='red'>该用户名已经被使用</font>";
        } else {
            result = "<font color='green'>该用户名可以使用</font>";
        }

        LOGGER.info(Instant.now() + ":" + result);

        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print(result);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
